package com.basic.util;

import java.io.File;
import java.util.TimeZone;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

public class DeviceTool {
	
	public static final int ANDROID_1_5 = 3; 
	public static final int ANDROID_1_6 = 4; 
	public static final int ANDROID_2_0 = 5; 
	public static final int ANDROID_2_0_1 = 6; 
	public static final int ANDROID_2_1 = 7; 
	public static final int ANDROID_2_2 = 8; 
	public static final int ANDROID_2_3 = 9; 
	public static final int ANDROID_2_3_3 = 10; 
	public static final int ANDROID_3_0 = 11; 
	
	public static final int ROOT_STATUS_UNCHECK = -2;
	public static final int ROOT_STATUS_UNKNOWN = -1;
	public static final int ROOT_STATUS_FALSE= 0;
	public static final int ROOT_STATUS_TRUE = 1;
	
	private static final String TAG = "Env";
	private static final int SDK_LEVEL = Integer.parseInt(Build.VERSION.SDK);
	private static int rootStatus = ROOT_STATUS_UNCHECK;
	
	/**
	 * 获取当前版本
	 * @param context
	 * @return
	 */
	public static String getVersion(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionName;
		} catch (NameNotFoundException e) {
			return "null";
		}
	}
	
	/**
	 * 获取当前版本号 
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionCode;
		} catch (NameNotFoundException e) {
			return -1;
		}
	}
	/**
	 * @return 系统版本号
	 */
	public static int getSDKLevel(){
		return android.os.Build.VERSION.SDK_INT;
	}
	
	/**
	 * @return 系统版本
	 */
	public static String getSDK(){
		if(Build.VERSION.SDK != null)
			return Build.VERSION.SDK;
		return "null";
	}
	
	/**
	 * @return 设备名
	 */
	public static String getModels(){
		if(Build.MODEL != null)
		return Build.MODEL;
		return "null";
	}
	
	/**
	 * @return 时区
	 */
	public static int getTimeZone(){
		TimeZone timezone=TimeZone.getDefault();
		return timezone.getOffset(System.currentTimeMillis());
	}
	
	/**
	 * @param context
	 * @return 国家代码 086
	 */
	public static String getCounty(Context context){
//	String locale = context.getResources().getConfiguration().locale.getCountry();  
//	String locale = context.getResources().getConfiguration().locale.getDisplayCountry(); 
	TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
	String countryCode = tm.getSimCountryIso(); 
	if(countryCode == null)
		countryCode = "null";
	return countryCode;
	}
	
	/**
	 * @return rom名 atrix4g olympus
	 */
	public static String getRom(){
		if(Build.BOARD != null)
		return Build.BOARD;
		return "null";
	}
	
	/**
	 *	判断是否安装了市场类应用 
	 * @param context
	 * @return
	 */
	public static boolean hasMarket(Context context){
		Intent intent = new Intent(Intent.ACTION_VIEW, 
				Uri.parse("market://details?id=".concat(context.getPackageName())));
		return IntentTool.isIntentResoled(context, intent);
	}
	
	/**
	 * @return root状态
	 */
	public static int getRootStatus() {
		if(rootStatus == ROOT_STATUS_UNCHECK) {
			String pathStr = System.getenv("PATH");
			String[] paths;
			
			if(pathStr == null) {
				paths = new String[] {"/sbin", "/system/sbin", "/system/bin", "/system/xbin"};
				rootStatus = ROOT_STATUS_UNKNOWN;
			} else {
				paths = pathStr.split(File.pathSeparator);
				rootStatus = ROOT_STATUS_FALSE;
			}
			
			for (String path : paths) {
				File suFile = new File(path + File.separatorChar + "su");
				try {
					if(suFile.isFile()) {
						rootStatus = ROOT_STATUS_TRUE;
						break;
					}
				} catch(SecurityException e) {
					Log.w(TAG, "", e);
					rootStatus = ROOT_STATUS_UNKNOWN;
				}
			}				
		}
		return rootStatus;
	}
	
}
